package com.haoze.nurseapp.controller;

import com.github.pagehelper.PageInfo;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurseapp.annotation.NurseActionLog;
import com.haoze.nurseapp.dto.StopTaskDTO;
import com.haoze.nurseapp.dto.TaskNodeDTO;
import com.haoze.nurseapp.model.AtomizeMedReturnEntity;
import com.haoze.nurseapp.model.FileIndexDataEntity;
import com.haoze.common.model.OrdersFileIndexDataEntity;
import com.haoze.nurseapp.service.TaskExecuteService;
import com.haoze.nurseapp.service.TransfusionService;
import com.haoze.nurseapp.dto.TaskFileFromViewDTO;
import com.haoze.nurseapp.vo.TaskNodeVO;
import com.haoze.nurseapp.service.UserService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * yek
 * 2020.03.06
 * @author embed
 */
@RestController
@RequestMapping(value = "/nurseapp/taskExecute")
@Api(value = "任务流执行")
public class TaskExecuteController {

    @Resource
    TaskExecuteService taskExecuteService;

    @Resource
    TransfusionService transfusionService;

    @Resource
    UserService userService;

    @GetMapping("/patientOrders")
    @ApiOperation(value = "根据条形码编号查询医嘱及药品信息")
    @NurseActionLog(value = "扫码条码")
    public Result getDrugsByQrcode(@RequestParam String firstQrCode, @RequestParam String nodeId, @RequestParam String type,@RequestParam String secondQrCode){
        return taskExecuteService.getDrugsByQrcode(firstQrCode, nodeId, type, secondQrCode);
    }

    @PostMapping("/doProcess")
    @ApiOperation(value = "流程节点推送")
    @NurseActionLog(value = "流程节点推送")
    public Result pushProcess(@RequestBody Map<String,Object> map) {
        return taskExecuteService.doProcess(map);
    }

    @GetMapping("/getWorkbenchInfo")
    @ApiOperation("工作台目录对应任务节点ID")
    public Result getWorkbenchInfo(){
        return taskExecuteService.getWorkbenchInfo();
    }

    @GetMapping("/getTaskNum")
    @ApiOperation("任务汇总数据")
    public Result getTaskNum(@RequestParam String nodeId,@RequestParam String currentDate,@RequestParam String wardId) {
        List<Map<String,String>> taskNumList = taskExecuteService.queryTaskNum(nodeId,currentDate,wardId);
        return ResultGenerator.genOkResult(taskNumList);
    }

    @PostMapping("/getTasks")
    @ApiOperation("获取任务列表")
    public Result getTasks(@RequestBody Map<String,Object> map) {
        PageInfo<Map<String,Object>> tasksList = taskExecuteService.getTasks(map);
        return ResultGenerator.genOkResult(tasksList);
    }

    @PostMapping("/copyStartNode")
    @ApiOperation(value = "复制初始节点")
    public Result copyStartNode(@RequestBody Map<String,Object> map) {
        return taskExecuteService.createBatchQrcode(map);
    }

    @GetMapping("/doInfusion")
    @ApiOperation("扫描药品二维码获取当前医嘱的输液信息")
    public Result doInfusion(@RequestParam String qrCode) {
        List<OrdersFileIndexDataEntity> orderInfoList = taskExecuteService.getOrderInfoByBarCode(qrCode);
        return ResultGenerator.genOkResult(orderInfoList);
    }

    @GetMapping("/getPiercing")
    @ApiOperation("获取穿刺，通道及换液信息")
    public Result getPiercing(@RequestParam String qrCode,@RequestParam String nodeName){
        return transfusionService.queryFileIdByBarCode(qrCode,nodeName);
    }

    @PostMapping("/savePiercing")
    @ApiOperation("保存穿刺，通道及换液")
    public Result savePiercing(@RequestBody TaskFileFromViewDTO taskFileFromViewDTO) {
        return transfusionService.savePiercing(taskFileFromViewDTO);
    }

    @GetMapping("/getAllInfusion")
    @ApiOperation("获取当前患者的所有输液信息")
    @NurseActionLog("获取输液信息")
    public Result getAllInfusion(@RequestParam String qrCode, @RequestParam String inpatNum) {
        List<Map<String,Object>> emrFileList = transfusionService.getAllInfsion(qrCode,inpatNum);
        return ResultGenerator.genOkResult(emrFileList);
    }

    @PostMapping("/endInfusion")
    @ApiOperation("结束输液")
    public Result endInfusion(@RequestBody Map<String,Object> qrCodeMap) {
        transfusionService.saveEndInfusion(qrCodeMap);
        return ResultGenerator.genOkResult();
    }

    @PostMapping("/endInfusionNew")
    @ApiOperation("结束输液")
    public Result endInfusionNew(@RequestBody Map<String, Object> paramMap) {
        transfusionService.saveEndInfusionNew(paramMap);
        return ResultGenerator.genOkResult();
    }

    @PostMapping("/markBackDrug")
    @ApiOperation("标记退药")
    public Result markBackDrug(@RequestBody Map<String,String> qrCodeMap) {
        return transfusionService.markBackDrug(qrCodeMap);
    }

    @GetMapping("/getNodeState")
    @ApiOperation("获取节点状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "二维码编码", dataType = "String", paramType = "query", example = "20200326000580005", defaultValue = "20200326000580005")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseContainer = "1:穿刺完成，0:未穿刺")
    })
    public Result getNodeState(@RequestParam String qrCode) {
        return transfusionService.getNodeState(qrCode);
    }

    @GetMapping("/getOrderInfo")
    @ApiOperation("获取医嘱信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "二维码编码", dataType = "String", paramType = "query", example = "20200326000580005", defaultValue = "20200326000580005")
    })
    public Result getOrdersInfo(@RequestParam String qrCode) {
        return taskExecuteService.getOrdersInfo(qrCode);
    }


    @GetMapping("/getRedrugsDic")
    @ApiOperation("获取退药原因")
    public Result getReturnDic() {
        List<Map<String,String>> reDrugs = transfusionService.getReturnDic();
        return ResultGenerator.genOkResult(reDrugs);
    }

    @PostMapping("/saveFile")
    @ApiOperation("保存输液文书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskFileFromViewDTO", value = "文书内容", dataType = "TaskFileFromViewDTO")
    })
    public Result saveFile(@RequestBody TaskFileFromViewDTO taskFileFromViewDTO) {
        return transfusionService.saveFile(taskFileFromViewDTO);
    }

    @PostMapping("/infusionInspection")
    @ApiOperation("输液中心巡视")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "二维码编码", dataType = "String", paramType = "query", example = "20200326000580005", defaultValue = "20200326000580005")
    })
    public Result infusionInspection(@RequestBody Map<String, String> map) {
        return transfusionService.infusionInspection(map.get("qrCode"));
    }

    @PostMapping("/pauseInfusion")
    @ApiOperation("暂停输液")
    public Result pauseInfusion(@RequestBody Map<String, Object> param) {
        return transfusionService.pauseInfusion(param);
    }

    @GetMapping("/queryDictByVarCode")
    @ApiOperation("获取数据元下拉")
    public Result queryDictByVarCode(@RequestParam("varCode") String varCode) {
        return transfusionService.queryDictByVarCode(varCode);
    }

    @GetMapping("/getSurplus")
    @ApiOperation("获取剩余量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emrFileId",value = "文书ID",dataType = "String",example = "dd6c2685f5f54c15bb0a7f7bcc3f362e",defaultValue = ""),
            @ApiImplicitParam(name = "speed",value = "滴速",dataType = "String",example = "45",defaultValue = "")
    })
    public Result getSurplus(@RequestParam("emrFileId") String emrFileId,@RequestParam("speed") String speed){
        return transfusionService.getSurplus(emrFileId,speed);
    }

    @GetMapping("/createChannel")
    @ApiOperation("新增通道获取穿刺信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preQrcode",value = "穿刺信息qrCode",dataType = "String",example = "2020041000022_1_1",defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = FileIndexDataEntity.class)
    })
    public Result createChannel(@RequestParam("preQrcode") String preQrcode){
        return transfusionService.createChannel(preQrcode);
    }

    @GetMapping("/testPush")
    @ApiOperation("测试推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "测试消息推送", dataType = "String", example = "ceshi", defaultValue = "")
    })
    public void testPush(@RequestParam("message") String message) {
//        AppPush.pushMessage("测试消息", message);
    }

    @GetMapping("/pushUser")
    @ApiOperation("推送到个人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", dataType = "String", example = "account", defaultValue = ""),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "String", example = "title", defaultValue = ""),
            @ApiImplicitParam(name = "message", value = "消息", dataType = "String", example = "message", defaultValue = "")
    })
    public void pushUser(@RequestParam("account") String account,@RequestParam("title") String title,@RequestParam("message") String message) {
//        UserEntity userEntity = userService.getUserByAccount(account);
//        if(null != userEntity && null != userEntity.getClientId()){
//            AppPush.pushUser(userEntity.getClientId(), title, message);
//        }
    }

    @GetMapping("/endInfusionInfo")
    @ApiOperation("输液信息展示及预计结束时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emrFileId",value = "文书ID",dataType = "String",example = "dd6c2685f5f54c15bb0a7f7bcc3f362e",defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = java.util.List.class)
    })
    public Result endInfusionInfo(@RequestParam String emrFileId){
        Map<String,Object> resultMap = transfusionService.endInfusionInfo(emrFileId);
        System.out.println(ResultGenerator.genOkResult(resultMap));
        return ResultGenerator.genOkResult(resultMap);
    }

    @GetMapping("/defaultPuncture")
    @ApiOperation("默认穿刺点信息返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode",value = "输液中心二维码编号",dataType = "String",example = "2020041100251_1_1",defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = java.util.Map.class)
    })
    public Result getDefaultPuncture(@RequestParam String qrCode){
        return transfusionService.getDefaultPuncture(qrCode);
    }

    @GetMapping("/endInfusionType")
    @ApiOperation("获取结束输液方式字典值")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = java.util.Map.class)
    })
    public Result getEndInfusionType(){
        return transfusionService.endInfusionType();
    }

    @GetMapping("/getMedicineNum")
    @ApiOperation("获取已完成未完成领药数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId",value = "病区ID",dataType = "String",example = "FA16E4AF308D4A50A4559B7238B8112C",defaultValue = ""),
            @ApiImplicitParam(name = "currentDate",value = "当前日期",dataType = "String",example = "2020-04-25",defaultValue = ""),
            @ApiImplicitParam(name = "currentTime",value = "频次时间",dataType = "String",example = "12:30",defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = java.util.Map.class)
    })
    public Result getMedicineNum(@RequestParam String wardId,@RequestParam String currentDate,@RequestParam String currentTime){
        return transfusionService.getMedicineNum(wardId,currentDate,currentTime);
    }

    // TODO: 通用性优化
    @GetMapping("/screenQrCode")
    @ApiOperation("根据条形码编号查询医嘱及药品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "第一个二维码", dataType = "String", example = "2020041100251_1_1", defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = TaskNodeVO.class)
    })
    public Result screenQrCode(@RequestParam String qrCode, @RequestParam String showFlag, @RequestParam(required = false) String ctlId) {
        return taskExecuteService.screenQrCode(qrCode, ctlId, showFlag);
    }

    // TODO: 通用性优化
    @GetMapping("/generatTaskNode")
    @ApiOperation("根据条形码编号查询医嘱及药品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "第一个二维码", dataType = "String", example = "2020041100251_1_1", defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = TaskNodeVO.class)
    })
    public Result generatTaskNode(@RequestParam String qrCode){
        return taskExecuteService.generatTaskNode(qrCode);
    }

    @GetMapping("/screenQrCodeCenter")
    @ApiOperation("根据条形码编号查询医嘱及药品信息")
    @NurseActionLog(value = "扫码条码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "第一个二维码", dataType = "String", example = "2020041100251_1_1", defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = TaskNodeVO.class)
    })
    public Result screenQrCodeInCenter(@RequestParam String qrCode) {
        return taskExecuteService.screenQrCodeInCenter(qrCode);
    }

    @PostMapping("/passNode")
    @ApiOperation(value = "节点确认及文书保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskNode", value = "节点数据", dataType = "TaskNodeDTO", example = "", defaultValue = "")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功")
    })
    public Result passNode(@RequestBody TaskNodeDTO taskNode) {
        return  taskExecuteService.passNode(taskNode);
    }





    @GetMapping("/getInfusions")
    @ApiOperation("获取当前患者的所有输液信息")
    @NurseActionLog("获取输液信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "进入输液中心的二维码", dataType = "string", example = "", defaultValue = "")
    })
    public Result getInfusions(@RequestParam String qrCode) {
        return ResultGenerator.genOkResult(transfusionService.getInfusions(qrCode));
    }

    @GetMapping("/infusionCenterList")
    @ApiOperation("输液中心-获取穿刺通道信息")
    @NurseActionLog("获取穿刺通道等输液信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "进入输液中心的二维码", dataType = "string", example = "", defaultValue = "")
    })
    public Result infusionCenterList(@RequestParam String qrCode) {
        return ResultGenerator.genOkResult(transfusionService.infusionCenterList(qrCode));
    }

    @GetMapping("/getWaitTasks")
    @ApiOperation("获取待执行任务")
    @NurseActionLog("获取输液信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "病人腕带/床头牌", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", example = "1", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页长", dataType = "int", example = "10", defaultValue = "10")
    })
    public Result getWaitTasks(@RequestParam String qrCode, @RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String word) {
        return ResultGenerator.genOkResult(taskExecuteService.getWaitTasks(qrCode, page, pageSize, word));
    }

    @GetMapping("/checkQrCode")
    @ApiOperation("核实二维码")
    @NurseActionLog("核实两个二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstQrCode", value = "第一个二维码", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "secondQrCode", value = "第二个二维码", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "nodeType", value = "节点入口", dataType = "string", example = "", defaultValue = "")
    })
    public Result checkQrCode(@RequestParam String firstQrCode, @RequestParam String secondQrCode, @RequestParam String nodeType, @RequestParam String showFlag,@RequestParam(required = false) String uuid) {
        return ResultGenerator.genOkResult(taskExecuteService.checkQrCode(firstQrCode, secondQrCode, nodeType, showFlag,uuid));
    }

    @GetMapping("/ruleFilter")
    @ApiOperation("规则过滤")
    @NurseActionLog("规则过滤")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emrFileId", value = "文书id", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "varCode", value = "节点位移标识", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "dataCode", value = "触发修改后的新code", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "dataValue", value = "触发修改后的新value", dataType = "string", example = "", defaultValue = "")
    })
    public Result ruleFilter(@RequestParam String emrFileId,
                             @RequestParam String varCode,
                             @RequestParam String dataCode,
                             @RequestParam String dataValue) {
        return ResultGenerator.genOkResult(taskExecuteService.ruleFilter(emrFileId, varCode, dataCode, dataValue));
    }

    @GetMapping("/workSpaceScreen")
    @ApiOperation(value = "工作台扫描二维码")
    @NurseActionLog(value = "工作台扫描二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "扫描的二维码", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "nodeId", value = "工作台入口节点", dataType = "string", example = "", defaultValue = "")
    })
    public Result workSpaceScreen(@RequestParam String qrCode, @RequestParam String nodeId){
        return taskExecuteService.workSpaceScreen(qrCode, nodeId);
    }

    @GetMapping("/getInfusionCenter")
    @ApiOperation("获取当前患者的所有输液信息")
    @NurseActionLog("获取输液信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "string", example = "", defaultValue = "")
    })
    public Result getInfusionCenter(@RequestParam String inpatNum) {
        return ResultGenerator.genOkResult(transfusionService.getInfusionCenter(inpatNum));
    }

    @GetMapping("/getWorkLog")
    @ApiOperation("获取工作日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "dateTime", value = "选中日期", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", example = "1", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页长", dataType = "int", example = "10", defaultValue = "10"),
            @ApiImplicitParam(name = "search", value = "搜索内容", dataType = "string", example = "加药", defaultValue = ""),
            @ApiImplicitParam(name = "type", value = "类型：0全部，1未完成，2已完成", dataType = "string", example = "0/1/2", defaultValue = "")
    })
    public Result getWorkLog(@RequestParam String inpatNum,
                             @RequestParam String dateTime,
                             @RequestParam int page,
                             @RequestParam int pageSize,
                             @RequestParam String search,
                             @RequestParam String type,
                             @RequestParam String orderType) {
        return ResultGenerator.genOkResult(taskExecuteService.getWorkLog(inpatNum, dateTime, page, pageSize, search, type,orderType));
    }


    @PostMapping("/stopTask")
    @ApiOperation(value = "没有生成任务时终止任务")
    public Result stopTask(@RequestBody StopTaskDTO stopTaskDTO) {
        return taskExecuteService.stopTask(stopTaskDTO);
    }


    @PostMapping("/queryTaskStatusByQrCode")
    @ApiOperation(value = "判断任务的状态")
    public Result queryTaskStatusByQrCode(@RequestBody StopTaskDTO stopTaskDTO) {
        return taskExecuteService.queryTaskStatusByQrCode(stopTaskDTO);
    }

    @PostMapping("/queryTaskPuncturePointByQrCode")
    @ApiOperation(value = "判断任务的状态")
    public Result queryTaskPuncturePointByQrCode(@RequestBody StopTaskDTO stopTaskDTO) {
        return taskExecuteService.queryTaskPuncturePointByQrCode(stopTaskDTO);
    }



    @GetMapping("/getWorkLogCount")
    @ApiOperation("获取工作日志计数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "string", example = "", defaultValue = ""),
            @ApiImplicitParam(name = "dateTime", value = "选中日期", dataType = "string", example = "", defaultValue = "")
    })
    public Result getWorkLogCount(@RequestParam String inpatNum,
                                  @RequestParam String dateTime) {
        return ResultGenerator.genOkResult(taskExecuteService.getWorkLogCount(inpatNum, dateTime));
    }

    @GetMapping("/getEmrFileByqrCode")
    @ApiOperation("根据qrCode获取对应基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "扫描的二维码", dataType = "string", example = "", defaultValue = ""),
    })
    public Result getEmrFileByqrCode(@RequestParam String qrCode){
        return taskExecuteService.getEmrFileByqrCode(qrCode);
    }

    @GetMapping("/deletePuncturePoint")
    @ApiOperation("删除穿刺点")
    public Result deletePuncturePoint(@RequestParam String inpatNum,
                                      @RequestParam String pointValue) {
        return ResultGenerator.genOkResult(taskExecuteService.deletePuncturePoint(inpatNum, pointValue));
    }

}
